package com.stylefeng.guns.modular.product.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.stylefeng.guns.core.util.ExcelUtil;
import com.stylefeng.guns.modular.product.model.*;
import com.stylefeng.guns.modular.product.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@Transactional
public class DataImportServiceImpl implements IDataImportService {

    @Autowired
    private IProductFxService productFxService;

    @Autowired
    private IOptionService optionService;

    @Autowired
    private IForwardService forwardService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISpotrefService spotrefService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean parseExcelFileToData(MultipartFile file) throws Exception {
        //根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
        boolean flag = false;
        Workbook hssfWorkbook = null;
        try {
            hssfWorkbook = ExcelUtil.getWorkBook(file);
            ImportParams params = new ImportParams();
            // 循环工作表Sheet
            for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
                String sheetName = hssfWorkbook.getSheetName(numSheet);
                params.setTitleRows(0);//表头在第几行
                params.setStartRows(0);//距离表头中间有几行不要的数据
                params.setStartSheetIndex(numSheet);//第几个sheet页
                params.setNeedVerfiy(true);//验证数据

                if(StringUtils.equals(sheetName,"FX")){
                    List<ProductFx> list = ExcelImportUtil.importExcel(file.getInputStream(), ProductFx.class, params);
                    if(null != list && list.size() > 0){
                        flag = productFxService.insertBatch(list);
                    }
                }else if(StringUtils.equals(sheetName,"Option")){
                    List<Option> list = ExcelImportUtil.importExcel(file.getInputStream(), Option.class, params);
                    if(null != list && list.size() > 0){
                        optionService.updateStatusOldOption(list);
                        flag =  optionService.insertBatch(list);
                    }
                }else if(StringUtils.equals(sheetName,"Forward")){
                    List<Forward> list = ExcelImportUtil.importExcel(file.getInputStream(), Forward.class, params);
                    if(null != list && list.size() > 0){
                        forwardService.updateStatusOldForward(list);
                        flag = forwardService.insertBatch(list);
                    }
                }else if(StringUtils.equals(sheetName,"Customer")){
                    List<Customer> list = ExcelImportUtil.importExcel(file.getInputStream(), Customer.class, params);
                    if(null != list && list.size() > 0){
                        flag = customerService.insertBatch(list);
                    }
                }else if(StringUtils.equals(sheetName, "SpotRef")){
                    List<Spotref> list = ExcelImportUtil.importExcel(file.getInputStream(), Spotref.class, params);
                    if(null != list && list.size() > 0){
                        flag = spotrefService.insertBatch(list);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return flag;
    }
}
